<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="订单号" prop="tradeSn">
            <el-input
              v-model="queryParams.activityName"
              placeholder="活动名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!--            <el-form-item label="优惠券类型" prop="couponType">-->
          <!--                <el-select-->
          <!--                    v-model="queryParams.couponType"-->
          <!--                    placeholder="优惠券类型"-->
          <!--                    clearable-->
          <!--                    size="small"-->
          <!--                >-->
          <!--                    <el-option-->
          <!--                        v-for="dict in couponTypeOptions"-->
          <!--                        :key="dict.dictValue"-->
          <!--                        :label="dict.dictLabel"-->
          <!--                        :value="dict.dictValue"-->
          <!--                    />-->
          <!--                </el-select>-->
          <!--            </el-form-item>-->
          <!--            <el-form-item label="是否限购" prop="isCanBuy">-->
          <!--                <el-select-->
          <!--                    v-model="queryParams.isCanBuy"-->
          <!--                    placeholder="优惠券是否限购"-->
          <!--                    clearable-->
          <!--                    size="small"-->
          <!--                >-->
          <!--                    <el-option-->
          <!--                        v-for="dict in isCanBuyOptions"-->
          <!--                        :key="dict.dictValue"-->
          <!--                        :label="dict.dictLabel"-->
          <!--                        :value="dict.dictValue"-->
          <!--                    />-->
          <!--                </el-select>-->
          <!--            </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permisaction="['activity:activity:add']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permisaction="['activity:activity:edit']"
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permisaction="['activity:activity:remove']"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="ymactivityList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="封面图" width="200px" align="center" prop="cover"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <!--                            <div v-for="item in scope.row.storeInfo" class="row ali_cen" :key="item.storeId">-->
                    <el-image style="width: 100px; height:100px;margin-right:10px"
                              :src="scope.row.cover">
                    </el-image>


                    <!--                            </div>-->
                </template>
            </el-table-column>
<!--          <el-table-column label="活动id" align="center" prop="activityId" :show-overflow-tooltip="true" />-->
            <el-table-column label="封面图" align="center" prop="cover" :show-overflow-tooltip="true" />
          <el-table-column label="活动名称" align="center" prop="activityName" :show-overflow-tooltip="true" />
          <el-table-column label="标签" align="center" prop="label" :show-overflow-tooltip="true" />
          <el-table-column label="售价" align="center" prop="price" :show-overflow-tooltip="true" />
          <el-table-column label="原价" align="center" prop="normalPrice" :show-overflow-tooltip="true" />
          <el-table-column label="排序" align="center" prop="weigh" :show-overflow-tooltip="true" />

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-permisaction="['activity:activity:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                v-permisaction="['activity:activity:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px">
          <el-form ref="form" :model="form" :rules="rules" label-width="140px">

            <el-form-item label="活动名称" prop="activityName">
              <el-input v-model="form.activityName" placeholder="活动名称" />
            </el-form-item>
            <el-form-item label="标签" prop="label">
              <!-- <el-input v-model="form.label" placeholder="标签" /> -->
              <tag-label :val.sync="form.label" />
            </el-form-item>
            <el-form-item label="售价" prop="price">
              <el-input-number v-model="form.price" placeholder="售价" />
            </el-form-item>
            <el-form-item label="原价" prop="normalPrice">
              <el-input-number v-model="form.normalPrice" placeholder="原价" />
            </el-form-item>
            <el-form-item label="封面图" prop="cover">
              <uploadImg :img-url.sync="form.cover" :cut="false" />
              <!-- <el-input v-model="form.cover" placeholder="封面图" /> -->
            </el-form-item>
            <el-form-item label="排序序号" prop="weigh">
              <el-input-number v-model="form.weigh" placeholder="排序序号" />
            </el-form-item>
            <el-form-item label="使用次数" prop="useNum">
              <el-input-number v-model="form.useNum" placeholder="使用次数" />
            </el-form-item>
            <el-form-item label="选几个服务" prop="chooseNum">
              <el-input-number v-model="form.chooseNum" placeholder="选几个服务" />
            </el-form-item>
            <el-form-item label="已卖数量" prop="saleNum">
              <el-input-number v-model="form.saleNum" placeholder="已卖数量" />
            </el-form-item>

            <el-form-item label="选择兑换服务" prop="serviceNum">
              <el-select
                v-model="form.serviceId"
                :disabled="form.orderId"
                placeholder="选择服务"
                multiple
                @change="changeService"
              >
                <el-option
                  v-for="item in serviceList"
                  :key="item.serviceId"
                  :label="item.title"
                  :value="item.serviceId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="活动开始售卖时间" prop="actSaleStartTime">
              <el-date-picker
                v-model="form.actSaleStartTime"
                type="datetime"
                placeholder="选择日期"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-form-item label="活动结束售卖时间" prop="actSaleEndTime">
              <el-date-picker
                v-model="form.actSaleEndTime"
                type="datetime"
                placeholder="选择日期"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-form-item label="" prop="isActAddDate">
              <el-tooltip
                slot="label"
                class="item"
                effect="dark"
                content="活动使用时间是否是使用累加时间类型 T 就是在购买后累加一段时间"
                placement="top-start"
              >
                <div>时间累加</div>
              </el-tooltip>
              <el-radio-group v-model="form.isActAddDate">
                <el-radio
                  v-for="dict in isActAddDateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.isActAddDate == 'T'" label="累加多少个月" prop="addMonthNum">
              <el-input-number v-model="form.addMonthNum" placeholder="累加多少个月" />
            </el-form-item>
            <template v-else>
              <el-form-item label="使用开始时间" prop="actUseStartTime">
                <el-date-picker
                  v-model="form.actUseStartTime"
                  type="datetime"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
              <el-form-item label="使用结束时间" prop="actUseEndTime">
                <el-date-picker
                  v-model="form.actUseEndTime"
                  type="datetime"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </template>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-card>
    </template>
  </BasicLayout>
</template>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
    height: 600px;
    overflow: auto;
}
</style>
<script>
import {
  addYmActivity,
  delYmActivity,
  getYmActivity,
  listYmActivity,
  updateYmActivity
} from '@/api/activity/activity/index'
import { serviceList } from '@/api/user'
import tagLabel from '@/components/tags/tags.vue'

export default {
  name: 'Config',
  components: {
    tagLabel
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      isEdit: false,
      // 类型数据字典
      typeOptions: [],
      ymactivityList: [],
      isActAddDateOptions: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        activityName:
                    undefined
        // isCanBuy:
        // undefined,

      },
      // 表单参数
      form: {
        isActAddDate: 'F'
      },
      // 表单校验
      rules: {},
      serviceList: []

    }
  },
  created() {
    this.getList()
    this.getDicts('sys_bool').then(response => {
      this.isActAddDateOptions = response.data
    })
    this.getDicts('sys_bool').then(response => {
      this.isCanBuyOptions = response.data
    })
    // serviceList().then(res => {
    //     this.serviceList = res.data.list
    // })
    serviceList({ pageSize: 1000 }).then(res => {
      this.serviceList = res.data.list
    })
  },
  methods: {
    changeService(e) {
      console.log(e.length)
      this.form.serviceNum = e.length
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true
      listYmActivity(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.ymactivityList = response.data.list
        this.total = response.data.count
        this.loading = false
      }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {

        activityId: undefined,
        activityName: undefined,
        label: undefined,
        price: undefined,
        normalPrice: undefined,
        cover: undefined,
        weigh: undefined,
        saleNum: undefined,
        isActAddDate: 'F',
        addMonthNum: undefined,
        serviceNum: undefined,
        actSaleStartTime: undefined,
        actSaleEndTime: undefined,
        actUseStartTime: undefined,
        actUseEndTime: undefined,
        useNum: undefined,
        chooseNum: undefined
      }
      this.resetForm('form')
    },
    isActAddDateFormat(row) {
      return this.selectDictLabel(this.isActAddDateOptions, row.isActAddDate)
    },
    isCanBuyFormat(row) {
      return this.selectDictLabel(this.isCanBuyOptions, row.isCanBuy)
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加活动表'
      this.isEdit = false
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const activityId =
                row.activityId || this.ids
      getYmActivity(activityId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改活动表'
        this.isEdit = true
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.activityId !== undefined) {
            updateYmActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addYmActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.activityId || this.ids
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delYmActivity(Ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
